#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2014年10月16日

@author: chenyongbing
'''

import smtplib  
from email.mime.text import MIMEText  
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.image import MIMEImage
import re
from email.Header import Header 
import logging

  
def send_mail(mail_user='',mail_pass='',mail_host='',mail_postfix='',to_list='',sub='',content='',stype=None):  
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"  
    #msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
    msg = MIMEMultipart()  
    msg['Subject'] = sub  
    msg['From'] = mail_user 
    msg['To'] = ";".join(to_list)  
    if stype==None:
        msg.attach(MIMEText(content,_subtype='plain',_charset='gb2312'))  
    elif stype=='html':
        msg.attach(MIMEText(content, _subtype='html',_charset='gb2312')) 
    
    try:  
        server = smtplib.SMTP()  
        server.connect(mail_host)  
        server.login(mail_user,mail_pass)  
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        logging.info('发送成功')
        return {'status':True,'msgs':u'发送成功'  }
    except Exception, e:  
        logging.error('发送失败 %s'%str(e))
        return {'status':False,'msgs':u'发送失败 %s'%str(e)   }
def send_mail_with_atts(mail_user,mail_pass,mail_host,mail_postfix,to_list,sub,content,atts=[]):
    msg = MIMEMultipart()
    i = 0
    for att in atts:
        
        i+=1
        try:
            att_name = re.search('/([^\/]+)$',att).group(1).decode('utf-8','ignore')
        except:
            att_name = 'file-%s'%i
        #print att_name,type(att_name)
        #att=unicode(att,'utf-8')
        mail_att = MIMEText(open(att, 'rb').read(), 'base64', 'utf-8')
        #mail_att = MIMEText()
        #mail_att.add_header('Content-Disposition', 'attachment', filename=att_name.encode('gb2312'))
        mail_att["Content-Type"] = 'application/octet-stream'
        mail_att["Content-Disposition"] = 'attachment; filename=%s'%att_name.encode("gb2312")#这里的filename可以任意写，写什么名字，邮件中显示什么名字
        msg.attach(mail_att)
    
    #加邮件头
    msg['Subject'] = sub  
    msg['From'] = mail_user
    msg['To'] = ";".join(to_list) 

    


    #return
    #msg['To'] = to_list
    #发送邮件
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user,mail_pass)#XXX为用户名，XXXXX为密码
        server.sendmail(msg['from'], to_list,msg.as_string())
        server.quit()
        #print u'发送成功'
        return True
    except Exception, e:  
        print str(e) 
        #print u'发送失败'
        return False
    
if __name__=="__main__":
    from localConfig import LocalConfig
    myLocalConfig = LocalConfig()
    mail_user = myLocalConfig.email_user
    mail_pass = myLocalConfig.email_passwd
    mail_host = myLocalConfig.email_smtp
    mail_postfix = myLocalConfig.email_postfix
    to_list=['chen.yongbing@zhimatech.com']
    sub = 'test'
    content = 'test'
    send_mail(mail_user,mail_pass,mail_host,mail_postfix,to_list,sub,content)
    #atts = ['/Users/chenyongbing/Documents/workspace/zmExport/report/店铺数据核对_123_1413434363.xls']
    #send_mail_with_atts(mail_user,mail_pass,mail_host,mail_postfix,to_list,sub,content,atts=atts)
        